//
//  GlobalVar.m
//  atlas
//
//  Created by 你好 on 17/1/4.
//  Copyright © 2017年 xyxNav. All rights reserved.
//

#import "GlobalVar.h"
#import <AFNetworking.h>
#import "UserManager.h"
@implementation GlobalVar
{
    BaseViewController *_curVC;
    BOOL _bApiUrlInitialed;
}


+(GlobalVar *)shareInstance
{
    static GlobalVar * instance = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        instance = [[[self class] alloc] init];
    });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        [self createNetworkMoniter];
    }
    return self;
}

- (void)setResponderViewController:(BaseViewController *)vc
{
    _curVC = vc;
}

- (void)notifyEvent:(AppEventType)type object:(id)object
{
    if ([NSThread isMainThread]) {
        if (_curVC && [_curVC respondsToSelector:@selector(onEventAction:object:)]) {
            [_curVC onEventAction:type object:object];
        }
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (_curVC && [_curVC respondsToSelector:@selector(onEventAction:object:)]) {
                [_curVC onEventAction:type object:object];
            }
        });
    }
}


- (void)createNetworkMoniter
{
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        self.bNetworkInitialed = YES;
        [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingReachabilityDidChangeNotification object:@(status)];
        
        if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
            //            [[CookDataManager shareInstance] recoverCookUploading];
        }
        
        if (status != AFNetworkReachabilityStatusUnknown &&
            status != AFNetworkReachabilityStatusNotReachable) {
//            [[UserManager shareInstance] refreshToken:YES];
//            [UserManager shareInstance]lo
        }
    }];
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}


+(NSString *)getUUID
{
    CFUUIDRef uuidObj = CFUUIDCreate(nil);
    NSString *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(nil, uuidObj);
    CFRelease(uuidObj);
    return uuidString;
}


@end
